package com.qijian.flowable.controller;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

import com.qijian.flowable.domain.MatrixFieldVal;
import lombok.RequiredArgsConstructor;
import com.qijian.common.utils.mybatis.WhereEntityTool;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.Api;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.qijian.common.annotation.Log;
import com.qijian.common.core.controller.BaseController;
import com.qijian.common.core.domain.AjaxResult;
import com.qijian.common.enums.BusinessType;
import com.qijian.flowable.domain.MatrixField;
import com.qijian.flowable.domain.query.MatrixFieldQuery;
import com.qijian.flowable.service.IMatrixFieldService;
import com.qijian.common.utils.poi.ExcelUtil;
import com.qijian.common.core.page.TableDataInfo;

import javax.servlet.http.HttpServletResponse;

/**
 * 流程矩阵字段Controller
 *
 * @author qijian
 * @date 2022-02-08
 */
@Api(tags = "流程矩阵字段")
@RestController
@RequiredArgsConstructor
@RequestMapping("/flowable/matrixField")
public class MatrixFieldController extends BaseController {

    private final IMatrixFieldService matrixFieldService;

    /**
     * 查询流程矩阵字段列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:list')")
    @GetMapping("/list")
    public TableDataInfo list(MatrixFieldQuery query, IPage<MatrixField> page) {
        List<MatrixField> list = matrixFieldService.list(WhereEntityTool.invoke(query));
        list = list.stream().sorted(Comparator.comparing(MatrixField::getType)).collect(Collectors.toList());
        return getDataTable(list);
    }

    /**
     * 导出流程矩阵字段列表
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:export')")
    @Log(title = "流程矩阵字段", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public void export(MatrixFieldQuery query, HttpServletResponse response) {
        new ExcelUtil<>(MatrixField.class).exportExcel(response, matrixFieldService.list(WhereEntityTool.invoke(query)), "流程矩阵字段数据");
    }

    /**
     * 获取流程矩阵字段详细信息
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:query')")
    @GetMapping(value = "/{fieldId}")
    public AjaxResult getInfo(@PathVariable("fieldId") Long fieldId) {
        return AjaxResult.success(matrixFieldService.getById(fieldId));
    }

    /**
     * 新增流程矩阵字段
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:add')")
    @Log(title = "流程矩阵字段", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody List<MatrixField> matrixFields) {
        return toAjax(matrixFieldService.saveBatch(matrixFields));
    }

    /**
     * 修改流程矩阵字段
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:edit')")
    @Log(title = "流程矩阵字段", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody List<MatrixField> matrixFields) {
        matrixFieldService.saveOrUpdateMatrixField(matrixFields);
        return AjaxResult.success();
    }

    /**
     * 删除流程矩阵字段
     */
    @PreAuthorize("@ss.hasPermi('flowable:matrix:remove')")
    @Log(title = "流程矩阵字段", businessType = BusinessType.DELETE)
    @DeleteMapping("/{fieldIds}")
    public AjaxResult remove(@PathVariable List<Long> fieldIds) {
        return toAjax(matrixFieldService.removeByIds(fieldIds));
    }
}
